from Utils import *


def remain1(data):
    table = [[0, 0, 0, 1], [1, 3, 0, 1], [2, 5, 1, 0], [3, 4, 0, 1], [4, 7, 0, 1], [5, 2, 1, 0], [6, 6, 1, 0],
             [7, 1, 1, 0]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain2(data):
    table = [[0, 5, 1, 0], [1, 10, 0, 1], [2, 11, 1, 0], [3, 15, 1, 0], [4, 14, 0, 1], [5, 7, 1, 0], [6, 0, 0, 1],
             [7, 9, 1, 0],
             [8, 1, 1, 0], [9, 3, 0, 1], [10, 6, 0, 1], [11, 8, 0, 1], [12, 13, 0, 1], [13, 2, 1, 0], [14, 4, 1, 0],
             [15, 12, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain3(data):
    table = [[0, 7, 1, 0], [1, 1, 1, 0], [2, 0, 0, 1], [3, 23, 0, 1], [4, 18, 0, 1], [5, 15, 1, 0], [6, 8, 1, 0],
             [7, 20, 0, 1],
             [8, 13, 0, 1], [9, 11, 0, 1], [10, 3, 1, 0], [11, 4, 1, 0], [12, 16, 0, 1], [13, 5, 1, 0], [14, 14, 0, 1],
             [15, 21, 1, 0],
             [16, 17, 0, 1], [17, 22, 0, 1], [18, 6, 1, 0], [19, 12, 0, 1], [20, 9, 0, 1], [21, 19, 0, 1],
             [22, 2, 0, 1], [23, 10, 1, 0]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain4(data):
    table = [[0, 20, 1, 0], [1, 3, 1, 0], [2, 14, 1, 0], [3, 5, 1, 0], [4, 9, 0, 1], [5, 13, 0, 1], [6, 8, 0, 1],
             [7, 17, 0, 1],
             [8, 6, 1, 0], [9, 18, 0, 1], [10, 22, 1, 0], [11, 7, 0, 1], [12, 24, 1, 0], [13, 31, 1, 0], [14, 16, 0, 1],
             [15, 0, 0, 1],
             [16, 23, 0, 1], [17, 10, 0, 1], [18, 11, 0, 1], [19, 15, 1, 0], [20, 19, 0, 1], [21, 28, 0, 1],
             [22, 4, 0, 1], [23, 1, 0, 1],
             [24, 26, 0, 1], [25, 30, 0, 1], [26, 21, 1, 0], [27, 29, 0, 1], [28, 12, 0, 1], [29, 27, 1, 0],
             [30, 2, 1, 0], [31, 25, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain5(data):
    table = [[0, 16, 0, 1], [1, 24, 1, 0], [2, 6, 0, 1], [3, 17, 0, 1], [4, 2, 0, 1], [5, 1, 1, 0], [6, 14, 1, 0],
             [7, 33, 0, 1],
             [8, 31, 0, 1], [9, 32, 1, 0], [10, 7, 1, 0], [11, 8, 0, 1], [12, 20, 1, 0], [13, 0, 1, 0], [14, 39, 0, 1],
             [15, 29, 1, 0],
             [16, 34, 0, 1], [17, 10, 1, 0], [18, 27, 1, 0], [19, 3, 0, 1], [20, 19, 0, 1], [21, 18, 1, 0],
             [22, 22, 1, 0], [23, 4, 0, 1],
             [24, 37, 1, 0], [25, 9, 1, 0], [26, 13, 1, 0], [27, 38, 1, 0], [28, 12, 0, 1], [29, 21, 0, 1],
             [30, 23, 0, 1], [31, 25, 0, 1],
             [32, 11, 1, 0], [33, 5, 1, 0], [34, 26, 0, 1], [35, 35, 0, 1], [36, 28, 0, 1], [37, 30, 1, 0],
             [38, 15, 1, 0], [39, 36, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain6(data):
    table = [[0, 11, 1, 0], [1, 44, 1, 0], [2, 9, 0, 1], [3, 17, 0, 1], [4, 47, 0, 1], [5, 1, 1, 0], [6, 14, 1, 0],
             [7, 23, 0, 1],
             [8, 31, 0, 1], [9, 25, 0, 1], [10, 29, 1, 0], [11, 8, 0, 1], [12, 5, 1, 0], [13, 0, 1, 0], [14, 43, 1, 0],
             [15, 45, 1, 0],
             [16, 34, 0, 1], [17, 10, 1, 0], [18, 27, 1, 0], [19, 16, 1, 0], [20, 24, 1, 0], [21, 18, 1, 0],
             [22, 22, 1, 0], [23, 30, 1, 0],
             [24, 6, 0, 1], [25, 13, 0, 1], [26, 7, 0, 1], [27, 28, 1, 0], [28, 4, 0, 1], [29, 26, 1, 0],
             [30, 46, 1, 0], [31, 42, 1, 0],
             [32, 40, 0, 1], [33, 3, 1, 0], [34, 41, 1, 0], [35, 35, 0, 1], [36, 37, 0, 1], [37, 15, 0, 1],
             [38, 20, 1, 0], [39, 36, 0, 1],
             [40, 12, 1, 0], [41, 33, 1, 0], [42, 38, 1, 0], [43, 32, 1, 0], [44, 19, 0, 1], [45, 21, 0, 1],
             [46, 2, 1, 0], [47, 39, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def remain7(data):
    table = [[0, 11, 1, 0], [1, 44, 0, 1], [2, 8, 1, 0], [3, 38, 0, 1], [4, 20, 0, 1], [5, 4, 1, 0], [6, 9, 0, 1],
             [7, 23, 0, 1],
             [8, 1, 1, 0], [9, 25, 0, 1], [10, 29, 1, 0], [11, 47, 0, 1], [12, 5, 1, 0], [13, 45, 0, 1], [14, 43, 1, 0],
             [15, 35, 0, 1],
             [16, 7, 0, 1], [17, 40, 0, 1], [18, 31, 0, 1], [19, 21, 0, 1], [20, 24, 1, 0], [21, 32, 0, 1],
             [22, 54, 1, 0], [23, 30, 1, 0],
             [24, 14, 1, 0], [25, 13, 0, 1], [26, 17, 0, 1], [27, 28, 1, 0], [28, 36, 0, 1], [29, 55, 0, 1],
             [30, 22, 1, 0], [31, 42, 1, 0],
             [32, 27, 0, 1], [33, 51, 1, 0], [34, 49, 1, 0], [35, 46, 0, 1], [36, 53, 0, 1], [37, 15, 0, 1],
             [38, 16, 0, 1], [39, 26, 1, 0],
             [40, 12, 1, 0], [41, 18, 1, 0], [42, 50, 1, 0], [43, 34, 1, 0], [44, 19, 0, 1], [45, 41, 0, 1],
             [46, 2, 1, 0], [47, 39, 0, 1],
             [48, 3, 1, 0], [49, 10, 1, 0], [50, 52, 1, 0], [51, 0, 1, 0], [52, 6, 1, 0], [53, 33, 0, 1],
             [54, 37, 1, 0], [55, 48, 0, 1]]
    arr = toBit(data)
    size = len(arr)
    arr1 = [0 for i in range(size)]
    for i in range(size):
        if arr[i] == 0:
            arr1[table[i][1]] = table[i][2]
        else:
            arr1[table[i][1]] = table[i][3]
    return toBytes(arr1)


def TenMagic16_enc(input):
    table = [[0, 12], [1, 62], [2, 19], [3, 48], [4, 13], [5, 40], [6, 30], [7, 49], [8, 31], [9, 24], [10, 45],
             [11, 61], [12, 51], [13, 4], [14, 28], [15, 3], [16, 7], [17, 58], [18, 53], [19, 1], [20, 36],
             [21, 21], [22, 22], [23, 39], [24, 54], [25, 25], [26, 26], [27, 6], [28, 52], [29, 10], [30, 5],
             [31, 50], [32, 8], [33, 46], [34, 17], [35, 16], [36, 9], [37, 37], [38, 38], [39, 57], [40, 20],
             [41, 41], [42, 42], [43, 23], [44, 11], [45, 34], [46, 29], [47, 44], [48, 15], [49, 14], [50, 2],
             [51, 60], [52, 47], [53, 43], [54, 18], [55, 35], [56, 32], [57, 27], [58, 33], [59, 55], [60, 63],
             [61, 56], [62, 59], [63, 0]]
    arr = input
    arr1 = [0 for i in range(len(arr))]
    for i in range(len(table)):
        arr1[table[i][1]] = arr[table[i][0]]
    return toBytes(arr1)


def TenMagic16(data, data_len):
    key = "7d0069660c9b5d32074facf37c3738a1"

    split_num = data_len // 8
    remain = data_len % 8
    result = b""
    for i in range(split_num):
        data_bit = toBit(data[8 * i: 8 * i + 8])
        result += TenMagic16_enc(data_bit)
    switcher = {
        1: remain1,
        2: remain2,
        3: remain3,
        4: remain4,
        5: remain5,
        6: remain6,
        7: remain7
    }
    if remain != 0:
        result += switcher[remain](data[split_num * 8:])

    return result
